<?php
namespace App\Controller;

use Think\Cache;

class MemberController extends ApiController
{
      //会员地址信息显示
      public function address_list(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (empty($user_id)||!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $data=D('Gongying_delivery')->where("user_id={$user_id}")->select();

              if (empty($data)) {
                  json_Response(-200, '传输失败', $data=[], 1);
              } else {
                  json_Response(200, '传输成功', $data, 1);
              }
      }else{
              json_Response('-200','传输失败','null',2);
          }
      }
      //增加收货地址
      public  function address_add(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (empty($user_id)||!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $data=I('post.');
              $model=D('Gongying_delivery');
              if (empty($data['consignee'])||empty($data['tell'])||empty($data['shipping_address'])||($data['city_rel']=="")||empty($data['city_town'])){
                  json_Response(-200, '参数错误', null, 2);
              }
              if (!is_numeric($data['tell'])||!is_numeric($data['city_rel'])||!is_numeric($data['city_town'])){
                  json_Response(-200, '参数错误', null, 2);
              }
              $patten2="/^1[34578]\d{9}$/";//11位数字组成，验证手机号码
              if(!preg_match($patten2,$data['tell'])){
                  json_Response('-200','手机号格式不对','null',2);
              }
              $data['city_rel'] = D('City')->where("id={$data['city_rel']}")->getField('area');
              $data['city_town'] = D('City')->where("id={$data['city_town']}")->getField('area');
              $data['city_classify'] ='0';
              if(!empty($data['city_classify'])){
                  if (!is_numeric($data['city_classify'])){
                      json_Response(-200, '参数错误', null, 2);
                  }
                  $data['city_classify'] = D('City')->where("id={$data['city_classify']}")->getField('area');
              }


              if($model->add($data))
              {
                  json_Response(200, '添加收货地址成功', $data, 2);
              }else{
                  json_Response(-200, '添加收货地址失败', $data, 2);
              }

          }else{
              json_Response('-200','传输失败','null',2);
          }

    }
      //修改收货地址
      public function address_edit()
     {
         if(IS_POST) {
             $user_id=I('post.user_id');
             if (empty($user_id)||!is_numeric($user_id)) {
                 json_Response('-200','请先登录','null',2);
             }
             $count=D('User')->where("id={$user_id}")->count();
             if(!$count){
                 json_Response('-200','请先登录','null',2);
             }
             $data=I('post.');
             $model=D('Gongying_delivery');
             if (empty($data['id'])||empty($data['consignee'])||empty($data['tell'])||empty($data['shipping_address'])||($data['city_rel']=="")||empty($data['city_town'])){
                 json_Response(-200, '参数错误', null, 2);
             }
             if (!is_numeric($data['id'])||!is_numeric($data['tell'])||!is_numeric($data['city_rel'])||!is_numeric($data['city_town'])){
                 json_Response(-200, '参数错误', null, 2);
             }
             $patten2="/^1[34578]\d{9}$/";//11位数字组成，验证手机号码
             if(!preg_match($patten2,$data['tell'])){
                 json_Response('-200','手机号格式不对','null',2);
             }
             $data['city_rel'] = D('City')->where("id={$data['city_rel']}")->getField('area');
             $data['city_town'] = D('City')->where("id={$data['city_town']}")->getField('area');
             $data['city_classify'] ='0';
             if(!empty($data['city_classify'])){
                 if (!is_numeric($data['city_classify'])){
                     json_Response(-200, '参数错误', null, 2);
                 }
                 $data['city_classify'] = D('City')->where("id={$data['city_classify']}")->getField('area');
             }

             if(false!==$model->save($data))
             {
                 json_Response(200, '修改收货地址成功', $data, 2);
             }else{
                 json_Response(-200, '修改收货地址失败', $data, 2);
             }

         }else{
             json_Response('-200','传输失败','null',2);
         }

    }
      //删除收货地址
      public function address_del()
    {
        if(IS_POST) {
            $user_id=I('post.user_id');
            if (empty($user_id)||!is_numeric($user_id)) {
                json_Response('-200','请先登录','null',2);
            }
            $count=D('User')->where("id={$user_id}")->count();
            if(!$count){
                json_Response('-200','请先登录','null',2);
            }
            $data=I('post.');
            $model=D('Gongying_delivery');
            if (empty($data['id'])||!is_numeric($data['id'])){
                json_Response(-200, '参数错误', null, 2);
            }
            if(false!==$model->delete($data['id'])){
                json_Response(200, '删除收货地址成功', $data, 2);
            }else{
                json_Response(-200, '删除收货地址失败', $data, 2);
            }


        }else{
            json_Response('-200','传输失败','null',2);
        }

    }
      //省市
      public function pmc(){
        if(IS_POST) {
            // 验证
            if(F('pmcdata')){
                $data=F('pmcdata');
            }else{
                $data = D('city')->where("pid=1")->select();
                foreach ($data as $k => &$v) {
                    $v['city'] = D('city')->where("pid={$v['id']}")->select();
                }
                foreach ($data as $k => &$v ){
                    foreach ($v['city'] as $k1 =>&$v1){
                        $result = D('city')->where("pid={$v1['id']}")->select();
                        if(empty($result)){
                            $v1['city'][] = array('id'=>"0",'area'=>"",'pid'=>"0");
                        }else{
                            $v1['city'] = D('city')->where("pid={$v1['id']}")->select();
                        }

                    }
                }
                F('pmcdata',$data);
            }

            if (empty($data)) {
                json_Response(-200, '传输失败', $data=[], 1);
            } else {
                json_Response(200, '传输成功', $data, 1);
            }
        }else{
            json_Response('-200','传输失败','null',2);
        }
    }
     //设置默认地址
      public function default_address(){
          if(IS_POST) {
              $user_id=I('post.user_id');
              if (empty($user_id)||!is_numeric($user_id)) {
                  json_Response('-200','请先登录','null',2);
              }
              $count=D('User')->where("id={$user_id}")->count();
              if(!$count){
                  json_Response('-200','请先登录','null',2);
              }
              $data=I('post.');
              $model=D('Gongying_delivery');
              if (empty($data['id'])||!is_numeric($data['id'])){
                  json_Response(-200, '参数错误', null, 2);
              }
              $data['default_address']=1;
              D('Gongying_delivery')->where(array(
                  'id'=>array('neq',$data['id']),
                  'user_id'=>array('eq',$user_id)
              ))->setField('default_address',0);

              if(false!==$model->save($data)){
                  json_Response(200, '设置默认收货地址成功', $data, 2);
              }else{
                  json_Response(-200, '设置默认收货地址失败', $data, 2);
              }


          }else{
              json_Response('-200','传输失败','null',2);
          }
      }
}
